﻿using System;
using System.Collections.Generic;
using XCI.Extensions;

namespace XCI.Core
{
    public class WebApiBuilder
    {
        private readonly WebApi webApi;
        private string url;
        private object entity;
        private string method = "get";
        private bool isJson = false;
        private IDictionary<string, string> data;

        public WebApiBuilder(WebApi api)
        {
            this.webApi = api;
        }

        public WebApiBuilder BaseUrl(string baseUrl)
        {
            webApi.SetBaseUrl(baseUrl);
            return this;
        }

        public WebApiBuilder Timeout(TimeSpan timeout)
        {
            webApi.GetClient().Timeout = timeout;
            return this;
        }

        public WebApiBuilder AddDefaultHeader(IDictionary<string, string> headerData)
        {
            webApi.AddDefaultHeader(headerData);
            return this;
        }

        public WebApiBuilder Url(string urlx)
        {
            this.url = urlx;
            return this;
        }

        //public WebApiBuilder Headers(string key, string value)
        //{
        //    if (headers == null)
        //    {
        //        headers = new Dictionary<string, string>();
        //    }
        //    headers.Add(key, value);
        //    return this;
        //}

        public WebApiBuilder Data(Dictionary<string, string> kv)
        {
            if (data == null)
            {
                data = new Dictionary<string, string>();
            }
            foreach (var item in kv)
            {
                data.Add(item);
            }
            return this;
        }
        public WebApiBuilder Data(Dictionary<string, object> kv)
        {
            if (data == null)
            {
                data = new Dictionary<string, string>();
            }
            foreach (var item in kv)
            {
                data.Add(item.Key, item.Value.ToStringOrEmpty());
            }
            return this;
        }


        public WebApiBuilder Data(string key, string value)
        {
            if (data == null)
            {
                data = new Dictionary<string, string>();
            }
            data.Add(key, value);
            return this;
        }

        public WebApiBuilder Data(string key, object value)
        {
            return Data(key, value.ToStringOrEmpty());
        }


        public WebApiBuilder Get()
        {
            method = "get";
            return this;
        }

        public WebApiBuilder Post()
        {
            method = "post";
            return this;
        }

        public WebApiBuilder PostJson(object ent)
        {
            entity = ent;
            method = "post";
            isJson = true;
            return this;
        }

        public string AsString()
        {
            //get请求
            if (method.Equals("get"))
            {
                return webApi.Get(url, data);
            }
            //post请求
            return isJson ? webApi.PostJson(url, entity ?? data) : webApi.Post(url, data ?? entity);
        }

        public BoolMessage<T> AsObject<T>() where T : class
        {
            //get请求
            if (method.Equals("get"))
            {
                return webApi.Get<T>(url, data);
            }
            //post请求
            return isJson ? webApi.PostJson<T>(url, entity ?? data) : webApi.Post<T>(url, data ?? entity);
        }

        public BoolMessage<object> AsObject()
        {
            return AsObject<object>();
        }
    }
}
